const func = {
	api:'https://adminapi.xinzhuvip.com/api/',
	fileUrl:'https://xinzhu-1301398555.cos.ap-shanghai.myqcloud.com/wxapp/',
	map_arr:function(arr){
		let res = []
		arr.forEach((e,i) => {
			let obj = {}
			obj.value = i
			obj.text = e
			if(e){
				res.push(obj)
			}
		})
		return res
	},
	map_uniapp_arr:function(arr){
		arr.forEach((e) => {
			e.text = e.label
			e.value = e.id
		})
		return arr
	},
	map_uniapp_name_arr:function(arr){
		arr.forEach((e) => {
			e.text = e.name
			e.value = e.id
		})
		return arr
	},
	imgToBase64(imagePath, callback) {
		// 创建文件系统管理器实例
		const fileSystemManager = uni.getFileSystemManager();
		// 读取文件
		fileSystemManager.readFile({
			filePath: imagePath,
			encoding: 'base64',
			success: res => {
				callback(res.data);
			},
			fail: err => {
				console.error('读取文件失败', err);
				callback(null);
			}
		});
	},
	bigImage(richContent){
		// 判断含有图片
		if (richContent.indexOf("src") >= 0) {
			const imgs = [];
			richContent.replace(/]*src=[\'"]([^\'"]+)[^>]*>/gi, function (match, capture) {
				imgs.push(capture);
			})
			wx.previewImage({
				current: imgs[0], // 当前显示图片的http链接
				urls: imgs
			})
		}
	},
	bigImageArr(richContent,type){
		wx.previewImage({
			current: richContent[type], // 当前显示图片的http链接
			urls: richContent
		})
	},
	dateToTimestamp(data){
		let date = new Date()
		return Date.parse(data) / 1000
	},
	getTimestamp(){
		let timestamp = new Date().getTime()
		timestamp = timestamp.toString()
		return timestamp.slice(0,10)
	},
	onLink(url,id){
		uni.navigateTo({
			url: url+"?id="+id
		});
	},
	onImgStyle(html){
		let newContent =  html.replace(/<img[^>]*>/gi, (match) => {
			match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '')
			match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '')
			match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '')
			match = match.replace(/style\s*=\s*(["'])(?:(?!\1).)*\1/gi, '') // 去除 style=""这样的属性
			return match
		 })
		 // img标签添加style属性：max-width:100%;height:auto
		 return newContent.replace(/\<img/gi,
			'<img style="width:100%;height:auto;"'
		 )
	},
	isValidPhoneNumber(phoneNumber) {
	    const phoneRegex = /^1\d{10}$/;
	    return phoneRegex.test(phoneNumber);
	}
}
 
export default func;